clear all; clc; close all;

Fd = 40e3;
Td=1/Fd;
Tmod = 0.1;
t = 0:Td:Tmod-Td;

S=cos(2*pi*(500+100*rand(1,1))*t);
Sw = S .* parzenwin(length(S))';
Sbl = S .* bartlett(length(S))';
figure(1);
plot(t,S);

Y = fft(S)/length(S);
f = 0:1/Tmod:(1/Td - 1/Tmod);
Y = 2*abs(Y);

Yw = fft(Sw)/length(Sw);
Yw = 2*abs(Yw);

Ybl = fft(Sbl)/length(Sbl);
Ybl = 2*abs(Ybl);

figure(1)
plot(f,Y/max(Y),f,Yw/max(Yw),f,Ybl/max(Ybl));
xlim([500 600])
legend('No win','Parzen','Black')
% ylim([0 1])
xlabel('Frequency (Hz)')
ylabel('|S(f)|')

[S0,k] = max(Y(1:fix(length(Y)/2)));
fprintf('f0 = %0.2f',f(k));